extends Node2D

export (PackedScene) var Mod

var score=0

# Called when the node enters the scene tree for the first time.
func _ready():
	randomize()
	$TouchJoy.hideJoy()
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	$Player.update_velocity($TouchJoy._get_joy_velocity())
	pass
func _input(event):
	if event is InputEventScreenTouch:
		if event.pressed:
			$TouchJoy.showJoy()
			pass
		if !event.pressed:
			$TouchJoy.hideJoy()
			pass

func game_over():
	$ScoreTimer.stop()
	$ModTimer.stop()
	$HUD.show_game_over()
	$Music.stop()
	$DeathMusic.play()
	pass # Replace with function body.

func new_game():
	score=0
	$Player.start($StartPosition2D.position)
	$StartTimer.start()
	$HUD.update_score(score)
	$HUD.show_message("Get Ready")
	$Music.play()
	

func _on_ModTimer_timeout():
	$ModPath2D/ModSpawnLocation.set_offset(randi())
	
	var mod=Mod.instance()
	add_child(mod)
	
	var direction=$ModPath2D/ModSpawnLocation.rotation+ PI/2
	mod.position=$ModPath2D/ModSpawnLocation.position
	
	direction+=rand_range(PI/4,-PI/4)
	mod.rotation=direction
	
	mod.linear_velocity=Vector2(rand_range(mod.min_speed,mod.max_speed),0)
	mod.linear_velocity=mod.linear_velocity.rotated(direction)
	
	$HUD.connect("start_game",mod,"_on_start_game")
	pass # Replace with function body.


func _on_ScoreTimer_timeout():
	score+=1
	$HUD.update_score(score)
	pass # Replace with function body.


func _on_StartTimer_timeout():
	$ModTimer.start()
	$ScoreTimer.start()
	pass # Replace with function body.


func _on_HUD_start_game():
	new_game()
	pass # Replace with function body.
	
func get_velocity():
	return $TouchJoy._get_joy_velocity()
